package com.oweight.sagacious.payment.interfaces.controller;

import com.oweight.sagacious.api.model.msg.Message;
import com.oweight.sagacious.auth.client.annotation.IgnoreClientToken;
import com.oweight.sagacious.auth.client.annotation.IgnoreUserToken;
import com.oweight.sagacious.common.context.BaseContextHandler;
import com.oweight.sagacious.common.infrastructure.util.IPUtil;
import com.oweight.sagacious.payment.domain.model.WeChatPayRefundModel;
import com.oweight.sagacious.payment.domain.model.WeChatPayUnifiedOrder;
import com.oweight.sagacious.payment.domain.service.IWeChatPayManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 微信统一下单：Controller
 *
 * @author SAGE
 * @version v1 2019-11-15 11:50:05
 */
@IgnoreUserToken
@IgnoreClientToken

@RestController
@RequestMapping("/api/wx")
public class WeChatPayController {

    @Autowired
    private IWeChatPayManager weChatPayManager;

    /**
     * 下单
     *
     * @param order
     * @return com.oweight.sagacious.api.model.msg.Message
     * @author zhougm
     * @version 2019/11/15 13:41
     */
    @PostMapping("/order/{configId}")
    public Message order(@RequestBody WeChatPayUnifiedOrder order,
                         HttpServletRequest request, @PathVariable String configId) {
        BaseContextHandler.set("wx_pay_config_id", configId);

        String clientIp = IPUtil.getClientIp(request);
        return weChatPayManager.order(order, clientIp);
    }

    /**
     * 退款
     *
     * @param refundModel
     * @return
     */
    @PostMapping("/refund/{configId}")
    public Message refund(@RequestBody WeChatPayRefundModel refundModel, @PathVariable String configId) {
        BaseContextHandler.set("wx_pay_config_id", configId);

        return weChatPayManager.refund(refundModel);
    }
}
